'use strict';

const { BaseParameter, stateKeys } = require('../core/base_mongo_model');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const studyTimeLineSchema = new Schema({
    ...BaseParameter,
    /**
     * 标题
     */
    title: { type: String, default: '' },
    /**
     * 时间
     */
    date: { type: Date, default: new Date() },
    /**
     * 内容
     */
    content: { type: String, default: '' },
  });

  /**
   * 删除单个数据
   * @param id 要删除数据的ID
   * @param thoroughly 是否测地删除数据
   * @returns {*}
   */
  studyTimeLineSchema.statics.removeModel = function(id, thoroughly = false) {
    if (thoroughly) {
      return this.deleteOne({
        _id: mongoose.Types.ObjectId(id),
      });
    }
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      stateKey: stateKeys.DUSTBIN,
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 更新数据
   */
  studyTimeLineSchema.statics.updateModel = function(id, upData) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, upData);
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 从回收站还原
   */
  studyTimeLineSchema.statics.dustbinRestore = function(id) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      stateKey: stateKeys.NORMAL,
    });
  };

  return mongoose.model('StudyTimeLine', studyTimeLineSchema, 'studyTimeLines');
};
